package com.zmx.piglearn.utils

import android.content.Context

class FileHelper(
    private val context: Context
) {
    fun read(filename: String): String {
        val input = context.openFileInput(filename)
        val buf = ByteArray(1024)
        val sb = StringBuilder()
        var len = input.read(buf)
        while (len > 0) {
            sb.append(String(buf, 0, len))
            len = input.read(buf)
        }
        input.close()
        return sb.toString()
    }

    fun save (filename: String, content: String) {
        // 使用私有模式, 创建的文件只能被本应用访问, 还会覆盖原文件哦
        val output = context.openFileOutput(filename, Context.MODE_PRIVATE)
        output.write(content.toByteArray())
        output.close()
    }
}